package wallet

import (
	"fmt"
	"jgt-sdk/utils"
)

type aliapp struct{}

const (
	result_ok string = "10000"
)

// AliAppCheckResult 支付宝APP同步校验结果
type AliAppCheckResult struct {
	Result string `json:"result"`

	RecordID string `json:"record_id"`
	OrderID  string `json:"order_id"`
	Price    int64  `json:"price"`
	Code     string `json:"code"`
	Msg      string `json:"msg"`
	SubCode  string `json:"sub_code"`
	SubMsg   string `json:"sub_msg"`
}

func (a *aliapp) pay(h *Handle, params *PayParams) (map[string]string, error) {
	uslStr, err := h.GetURL(fmt.Sprintf("/srv/v1/alipay/app/%s/pay", params.Client))
	if err != nil {
		return nil, err
	}
	body := map[string]interface{}{
		"order_id": params.OrderId,
		"describe": params.Describe,
		"price":    params.Price,
		"tag":      params.Tag,
	}
	var result = map[string]string{}

	err = utils.PostJSONWithToken(h.getContext(), uslStr, h.as.GetTokenHandle().Get, body, &result)

	return result, err
}

func (a *aliapp) check(h *Handle, params *CheckParams) (*CheckResult, error) {
	result := new(CheckResult)
	uslStr, err := h.GetURL(fmt.Sprintf("/srv/v1/alipay/app/%s/check", params.Client))
	if err != nil {
		return result, err
	}
	body := map[string]interface{}{
		"result": params.Data,
	}
	data := new(AliAppCheckResult)
	err = utils.PostJSONWithToken(h.getContext(), uslStr, h.as.GetTokenHandle().Get, body, data)
	if err != nil {
		return result, err
	}

	result.Result = data.Result
	result.BillID = data.RecordID
	result.OrderID = data.OrderID
	result.Msg = data.Msg
	result.Detail = fmt.Sprintf("[%s]%s[%s]%s", data.Code, data.Msg, data.SubCode, data.SubCode)
	result.Price = data.Price

	return result, err
}
